import os
import requests
import shutil
from pathlib import Path

def download_file(url, local_path):
    """下载文件到指定路径"""
    response = requests.get(url, stream=True)
    response.raise_for_status()
    
    with open(local_path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)

def setup_static_resources():
    """设置静态资源"""
    # 创建必要的目录
    static_dir = Path('static')
    css_dir = static_dir / 'css'
    js_dir = static_dir / 'js'
    webfonts_dir = static_dir / 'webfonts'
    
    for dir_path in [static_dir, css_dir, js_dir, webfonts_dir]:
        dir_path.mkdir(parents=True, exist_ok=True)
    
    # 下载 Bootstrap CSS
    bootstrap_css_url = 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css'
    download_file(bootstrap_css_url, css_dir / 'bootstrap.min.css')
    
    # 下载 Font Awesome CSS
    fontawesome_css_url = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'
    download_file(fontawesome_css_url, css_dir / 'all.min.css')
    
    # 下载 Noto Sans SC 字体
    noto_sans_sc_css = '''
    /* Noto Sans SC */
    @font-face {
        font-family: 'Noto Sans SC';
        font-style: normal;
        font-weight: 300;
        src: url(https://fonts.gstatic.com/s/notosanssc/v36/k3kIo84MPvpLmixcA63oeALhLOCT-xWNm8Hqd37g1OkDRZe7lR4sg1IzSy-MNbE9VH8V.119.woff2) format('woff2');
    }
    @font-face {
        font-family: 'Noto Sans SC';
        font-style: normal;
        font-weight: 400;
        src: url(https://fonts.gstatic.com/s/notosanssc/v36/k3kIo84MPvpLmixcA63oeALhLOCT-xWNm8Hqd37g1OkDRZe7lR4sg1IzSy-MNbE9VH8V.119.woff2) format('woff2');
    }
    @font-face {
        font-family: 'Noto Sans SC';
        font-style: normal;
        font-weight: 500;
        src: url(https://fonts.gstatic.com/s/notosanssc/v36/k3kIo84MPvpLmixcA63oeALhLOCT-xWNm8Hqd37g1OkDRZe7lR4sg1IzSy-MNbE9VH8V.119.woff2) format('woff2');
    }
    @font-face {
        font-family: 'Noto Sans SC';
        font-style: normal;
        font-weight: 700;
        src: url(https://fonts.gstatic.com/s/notosanssc/v36/k3kIo84MPvpLmixcA63oeALhLOCT-xWNm8Hqd37g1OkDRZe7lR4sg1IzSy-MNbE9VH8V.119.woff2) format('woff2');
    }
    '''
    with open(css_dir / 'noto-sans-sc.css', 'w', encoding='utf-8') as f:
        f.write(noto_sans_sc_css)
    
    # 下载 jQuery
    jquery_url = 'https://code.jquery.com/jquery-3.6.0.min.js'
    download_file(jquery_url, js_dir / 'jquery.min.js')
    
    # 下载 Bootstrap JS
    bootstrap_js_url = 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js'
    download_file(bootstrap_js_url, js_dir / 'bootstrap.bundle.min.js')
    
    # 下载 Font Awesome 字体文件
    fontawesome_webfonts_url = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/webfonts/fa-solid-900.woff2'
    download_file(fontawesome_webfonts_url, webfonts_dir / 'fa-solid-900.woff2')

if __name__ == '__main__':
    setup_static_resources()
    print("静态资源设置完成！") 